package config

import (
	log "github.com/micro/go-micro/v2/logger"
	"cirrus-core/config/global_vars"
	"cirrus-core/config/nacos"
	"gopkg.in/ini.v1"
)

type EnvConfig struct {
	Nacos nacos.NacosConfig `ini:"nacos"`
}

func GetEnvConfigFile() string {
	resourcesPath := *global_vars.ResourcesPath
	env := *global_vars.Env
	path := resourcesPath + "/config-" + env + ".ini"
	return path
}

var envConfig = new(EnvConfig)

func GetEnvConfig() *EnvConfig {
	return envConfig
}

func LoadNacosConfig() *nacos.NacosConfig {
	return &envConfig.Nacos
}

func LoadEnvConfig() *EnvConfig {
	path := GetEnvConfigFile()
	cfg, err := ini.Load(path)

	if err != nil {
		log.Fatal("InitNacosConfig: ", path, ".  config err: ", err.Error())
	}
	err = cfg.MapTo(envConfig)
	if err != nil {
		log.Fatal("InitNacosConfig: ", path, ".  MapTo err: ", err.Error())
	}
	log.Info("Nacos Config File", path, envConfig)
	return envConfig
}
